/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.context.support;

import com.bluemartini.dna.DNAListArray;
import com.bluemartini.dna.DNAStringArray;
import com.bluemartini.dna.DynamicDomainEvent;
import com.bluemartini.dna.DynamicDomainListener;

public abstract class AbstractDynamicDomainListener implements DynamicDomainListener {
	
	private final DNAStringArray supportedDomains = new DNAStringArray();

	/**
	 * Creates a new abstract dynamic domain listener.
	 */
	public AbstractDynamicDomainListener() { }
	
	/**
	 * Creates a new dynamic domain listener supporting
	 * a single domain.
	 * 
	 * @param domainName the name of the dynamic domain
	 * this dynamic domain listener supports
	 */
	public AbstractDynamicDomainListener(String domainName) {
		supportedDomains.addElement(domainName);
	}
	
	/**
	 * Creates a new dynamic domain listener supporting
	 * all the given domains.
	 * 
	 * @param domainNames an array of domain names this
	 * this dynamic domain listener supports
	 */
	public AbstractDynamicDomainListener(String[] domainNames) {
		this.setSupportedDomains(domainNames);
	}
	
	/* (non-Javadoc)
	 * @see com.bluemartini.dna.DynamicDomainListener#dynamicDomainRequested(com.bluemartini.dna.DynamicDomainEvent)
	 */
	public final void dynamicDomainRequested(DynamicDomainEvent event) {
		event.setDomainValues(getDomainValues(event.getDomainElement().getName()));
	}
	
	/**
	 * Returns the domain values for the given domain name.
	 * 
	 * @param domainName the name of the domain whose domain values
	 * should be returned
	 * 
	 * @return a <code>DNAListArray</code> containing the domain values
	 * for the given <code>domainName</code>; may not be <code>null</code>
	 * 
	 */
	public abstract DNAListArray getDomainValues(String domainName);

	/**
	 * Returns an array of domains supported by this domain listener.
	 * 
	 * @see #setSupportedDomains(String[])
	 * @see com.bluemartini.dna.DynamicDomainListener#supportedDynamicDomains()
	 */
	public final DNAStringArray supportedDynamicDomains() {
		return new DNAStringArray(supportedDomains);
	}
	
	/**
	 * Sets the domains supported by this dynamic domain adapter.
	 * 
	 * @param supportedDomains an array of supported domains 
	 */
	public void setSupportedDomains(String[] supportedDomains) {

		this.supportedDomains.removeAllElements();
		this.supportedDomains.addElements(supportedDomains);
	}

}
